<?php
if(isset($_POST['getcomment'])){
	$connect=mysqli_connect("localhost","root","root","test");
	$context="select count(*) from comment";
	$query=$connect->query($context);
	$result=$query->fetch_array(MYSQLI_BOTH);
	$perNumber=5;//单页展示条数
    @$page=$_POST['getcomment'];
	$totalNumber=$result[0];
	$endNumber=$totalNumber-($page-1)*$perNumber;
	$startNumber=$endNumber-5;
	if($startNumber<=0){$startNumber=1;}
	if($endNumber<=0){$endNumber=5;}
    $context="select * from comment where `Id` between {$startNumber} and {$endNumber}";
	//echo $context;
    $query=$connect->query($context);
	if($query){
		$arr=array();
		$i=0;
		while($result=mysqli_fetch_row($query)){ 
			$arr[$i]=$result;
			$i++;
		}
		
		echo json_encode(
		array("status"=>"success","describe"=>"","response"=>$arr)
		);
	}else{
		echo json_encode(
		array("status"=>"danger","describe"=>"无数据","response"=>"")
		);
	}
	$connect->close();
	die();
}

if(isset($_POST['getpage'])){
	@$page=$_POST['getpage'];
	if($page==""||$page<=0){$page=1;}
	$connect=mysqli_connect("localhost","root","root","test");
	$context="select count(*) from comment";
	$query=$connect->query($context);
	$result=$query->fetch_array(MYSQLI_BOTH);
	$perNumber=5;//单页展示条数
	$totalNumber=$result[0];
	if($totalNumber<=0){
		echo json_encode(
		array("status"=>"danger","describe"=>"无数据","previous"=>0,"next"=>0)
		);
		$connect->close();
		die();
	}
    $totalPage=ceil($totalNumber/$perNumber);//四舍五入取一个亿作为总页数
	if ($page != 1) { 
		$pre_num=$page-1;
	}else{
		$pre_num=0;
	}
	if ($page<$totalPage) {
		$next_num=$page+1;
	}else{
	    $next_num=0;
	}
	$connect->close();
	$echo_arr=array(
		"status"=>"success",
		"describe"=>"页数信息请求成功",
		"previous"=>$pre_num,
		"next"=>$next_num,
	);
	echo json_encode($echo_arr);
	die();
}

if(isset($_POST['sendcomment'])){
	@$email=htmlspecialchars($_POST['email']);
	@$comment=htmlspecialchars($_POST['comment']);
	@$name=htmlspecialchars($_POST['vistorname']);
	if($email==""||$comment==""||$name==""){
		echo json_encode(
		array("status"=>"danger","describe"=>"参数缺省")
		);
		die();
	}
	$connect=mysqli_connect("localhost","root","root","test");
	$updated_time=$created_time=date("Y年m月d日 H:i:s");
	$context="insert into comment(name,email,comment,updated_time,created_time)values('{$name}','{$email}','{$comment}','{$updated_time}','{$created_time}')";
	$query=$connect->query($context);
	if($query){
		echo json_encode(
		array("status"=>"success","describe"=>"评论插入成功")
		);
	}else{
		echo json_encode(
		array("status"=>"danger","describe"=>"评论插入失败")
		);
	}
    $connect->close();
}
?>